package com.example.coreshopapp.keyboard;

import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.coreshopapp.R;
import com.example.coreshopapp.keyboard.widget.CustomKeyboard;

import java.util.ArrayList;
import java.util.Map;

public class NormalKeyBoardActivity extends AppCompatActivity {

    private CustomKeyboard mNumKeyboard;
    private GridView gridView;
    private ArrayList<Map<String, String>> valueList;
    private EditText EditActivity;
    private Animation enterAnim;
    private Animation exitAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal_key_board);
        initAnim();
        initView();
        valueList = mNumKeyboard.getValueList();
    }

    /**
     * 数字键盘显示动画
     */
    private void initAnim() {
        enterAnim = AnimationUtils.loadAnimation(this, R.anim.push_bottom_in);
        exitAnim = AnimationUtils.loadAnimation(this, R.anim.push_bottom_out);
    }

    private void initView() {
        mNumKeyboard = (CustomKeyboard) findViewById(R.id.id_numkeyboard);
        mNumKeyboard.getLayoutBack().setOnClickListener(v -> {
            mNumKeyboard.startAnimation(exitAnim);
            mNumKeyboard.setVisibility(View.GONE);
            Log.i("mNumKeyboard:","GONE");
        });

        gridView = mNumKeyboard.getGridView();
        gridView.setOnItemClickListener(onItemClickListener);
        EditActivity.setOnClickListener(new NumOnClickListener());

    }

    private class NumOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mNumKeyboard.setFocusable(true);
            mNumKeyboard.setFocusableInTouchMode(true);
            mNumKeyboard.startAnimation(enterAnim);
            mNumKeyboard.setVisibility(View.VISIBLE);
            Log.i("mNumKeyboard:","VISIBLE");
        }
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position < 11 && position != 9) {    //点击0~9按钮
                int mStar = 0;
                int mEnd = 0;
                if (EditActivity.isFocused()) {
                    final int selStart = EditActivity.getSelectionStart();
                    final int selEnd = EditActivity.getSelectionEnd();
                    mStar = Math.max(0, Math.min(selStart, selEnd));
                    mEnd = Math.max(0, Math.max(selStart, selEnd));
                }
                final CharSequence selectedText = EditActivity.getText().subSequence(mStar, mEnd);
                String selectStr = selectedText.toString();
                String content = EditActivity.getText().toString().trim();
                if ((mStar != mEnd) && (selectStr != ""))
                {
                    String mLeftStr = "";
                    if (mStar > 0) mLeftStr = content.substring(0,mStar);
                    String mRightStr ="";
                    if (mEnd < content.length())  mRightStr = content.substring(mEnd,content.length());
                    content = mLeftStr + valueList.get(position).get("name") + mRightStr;
                    EditActivity.setText(content);
                    EditActivity.setSelection(mStar+1);
                }
                else {
                    //content = content + valueList.get(position).get("name");
                    int index = EditActivity.getSelectionStart();
                    Editable editable = EditActivity.getText();
                    editable.insert(index, valueList.get(position).get("name"));
                }
            } else {
                if (position == 9) {      //点击小数点
                    String content = EditActivity.getText().toString().trim();
                    if (!content.contains(".")) {
                        int index = EditActivity.getSelectionStart();
                        Editable editable = EditActivity.getText();
                        editable.insert(index, valueList.get(position).get("name"));
                    }
                }

                if (position == 11) {      //点击退格键
                    String content = EditActivity.getText().toString().trim();
                    if (content.length() > 0) {
                        int index = EditActivity.getSelectionStart();
                        if (index > 0) {
                            Editable editable = EditActivity.getText();
                            editable.delete(index - 1, index);
                        }
                    }
                }
            }
        }
    };
}
